package com.zhijian.medical.controller;

import com.zhijian.medical.annotation.CurrentUser;
import com.zhijian.medical.annotation.Response;
import com.zhijian.medical.entity.dto.PersonDto;
import com.zhijian.medical.entity.po.Agentia;
import com.zhijian.medical.entity.vo.request.AgentiaSearchVo;
import com.zhijian.medical.entity.vo.response.AgentiaSimpleVo;
import com.zhijian.medical.entity.vo.response.AgentiaVo;
import com.zhijian.medical.service.AgentiaService;
import com.zhijian.medical.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 试剂表 控制层
 *
 * @author HCG
 * @version 1.0
 * @date 2022-01-13 15:33:49
 */
@Slf4j
@Controller
@Api(tags = "试剂表")
public class AgentiaController {

    @Autowired
    private AgentiaService service;


    @Response
    @PostMapping("shila/add")
    @ApiOperation("新增石蜡")
    public boolean insertShila(@RequestBody Agentia.ShilaEntity shilaEntity) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), shilaEntity);
        Date now = new Date();
        Agentia param = new Agentia();
        param.setNumber(shilaEntity.getNumber());
        param.setName("石蜡");
        param.setBatchNumber(shilaEntity.getNumber());
        param.setBrand("皓石");
        param.setStock(5);
        param.setPrice(new BigDecimal("0"));
        param.setStorageDate(new SimpleDateFormat("yyyy-MM-dd").format(now));
        param.setCategoryId(40L);
        param.setGroupId(8L);
        param.setCreateTime(now);
        param.setUpdateTime(now);
        return service.insert(param) != null;
    }


    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("agentia")
    @ApiOperation("新增")
    public boolean insert(@RequestBody Agentia param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return service.insert(param) != null;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("agentia/{id}")
    @ApiOperation("删除")
    public boolean delete(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        service.deleteById(id);
        return true;
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("agentia/{id}")
    @ApiOperation("修改")
    public boolean update(@PathVariable("id") Long id, @RequestBody Agentia param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return service.update(param);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("agentia/{id}")
    @ApiOperation("根据id查询")
    public AgentiaVo selectById(@CurrentUser @ApiIgnore PersonDto personDto, @PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectVoById(personDto.getRoleIds(), id);
    }

    /**
     * 查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("agentia/page")
    @ApiOperation("列表查询")
    public Page<AgentiaVo> selectByCondition(@CurrentUser @ApiIgnore PersonDto personDto,
                                             @RequestBody(required = false) AgentiaSearchVo param) {
        if (param == null)
            param = AgentiaSearchVo.builder().build();
//        param.setGroupIds(personDto.getGroupIds());
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectPage(param);
    }

    /**
     * 出库
     *
     * @param id
     * @param count
     * @return
     */
    @Response
    @GetMapping("agentia/delivery/{id}")
    @ApiOperation("出库")
    public boolean delivery(@PathVariable("id") Long id,
                            @ApiParam(name = "count", value = "出库数目") @RequestParam(value = "count") Integer count) {
        return service.delivery(id, count);
    }

    /**
     * 盘点
     *
     * @param id
     * @param stock
     * @return
     */
    @Response
    @GetMapping("agentia/check/stock/{id}")
    @ApiOperation("盘点")
    public boolean checkStock(@PathVariable("id") Long id,
                              @ApiParam(name = "stock", value = "库存数量") @RequestParam(value = "stock") Integer stock) {
        return service.checkStock(id, stock);
    }

    /**
     * 获取包含批号与生产商的信息
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("agentia/simple/{id}")
    @ApiOperation("获取包含批号与生产商的信息")
    public AgentiaSimpleVo selectAgentiaSimpleVo(@PathVariable("id") Long id) {
        return service.selectAgentiaSimpleVo(id);
    }

}